package com.example.pattern.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

class FileUtils {
    suspend fun saveBitmap(bitmap: Bitmap)= withContext(Dispatchers.IO){
        val name = TimeUtils.timeName()
        val path = "${Helper.mContext.get()!!.filesDir}/$name"
        BufferedOutputStream(FileOutputStream(path)).use {bos->
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos)
        }
        path
    }
    suspend fun loadBitmapFromPath(path:String):Bitmap = withContext(Dispatchers.IO){
        BitmapFactory.decodeFile(path)
    }
    suspend fun deleteFile(path:String) = withContext(Dispatchers.IO){
        File(path).also{
            if(it.exists()) it.delete()
        }
    }
}